﻿using System;
using System.Collections.Generic;
using System.Text;
using DefectTracker.Common;
using System.Configuration;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;

namespace DefectTracker.WebSecurity
{
    internal class RemotingHelper
    {
        private TcpClientChannel m_TcpClientChannel;
        private string m_SecurityUrl;

        private string SecurityUrl
        {
            get { return m_SecurityUrl; }
            set { m_SecurityUrl = value; }
        }
        private ISecurity m_Security;

        public ISecurity Security
        {
            get { return m_Security; }
        }
        private static RemotingHelper m_Instance;

        internal static RemotingHelper Instance
        {
            get 
            {
                if (RemotingHelper.m_Instance == null)
                    RemotingHelper.m_Instance = new RemotingHelper();
                return RemotingHelper.m_Instance; 
            }
        }

        private RemotingHelper()
        {
            SecurityUrl = ConfigurationManager.AppSettings["SecurityUrl"];
            if (string.IsNullOrEmpty(SecurityUrl))
                throw new ConfigurationErrorsException("Configuration setting \"SecurityUrl\" not found or is not valid in \"App.Config\"");
            
            m_TcpClientChannel = new TcpClientChannel();
            ChannelServices.RegisterChannel(m_TcpClientChannel);
            m_Security = (ISecurity)Activator.GetObject(typeof(ISecurity), SecurityUrl + "\\Security");
        }
    }
}
